<template>
	<view class="layout" @click.stop="maskClosePopup" @touchmove.stop.prevent v-if="isPopup">
		<view class="layout-main" @click.prevent.stop>
			<view
				class="layout-main-content"
				:style="{
					backgroundImage: 'url(' + bgImage + ')'
				}"
			>
				<view class="layout-main-content-hint">
					恭喜你！获得{{ cycleLabel }}
					<text>{{ downloadCount }}</text>
					次素材圈 免费下载素材次数
				</view>

				<view class="layout-main-content-button" v-if="isButton">
					<button @click="openMaterial">去下载</button>
				</view>
			</view>

			<view class="layout-main-close">
				<image @click.prevent.stop="maskClosePopup" src="@/static/icon/icon_service_close.png" />
			</view>
		</view>
	</view>
</template>

<script>
import { defaultImageUrl } from '@/utils';

const bgImage = Object.freeze(`${defaultImageUrl}image/img_bg_invite.png`);

const cycleTypeList = [
	{
		type: 1,
		label: '每日'
	},
	{
		type: 2,
		label: '每周'
	},
	{
		type: 3,
		label: '每月'
	},
	{
		type: 4,
		label: '每季度'
	},
	{
		type: 5,
		label: '每年'
	},
	{
		type: 9,
		label: ''
	}
];

export default {
	name: 'Distribution-Invitation',

	props: {
		/**
		 * {
		 * 	codeUseStatus: 邀请码使用提示：-1邀请码不存在；0可以使用；1使用成功；2已失效；3重复使用；4已达使用上限；
		 * 	cycleType: 	福利周期：1每日；2每周；3每月；4每季度；5每年；9一次性；
		 * 	download: 邀请码使用成功后的下载次数
		 * }
		 */
		value: {
			type: Object,
			default: () => {
				return {};
			}
		},

		isButton: {
			type: Boolean,
			default: true
		},

		jumpRoute: {
			type: String,
			default: '/pages/tabBar/Material'
		}
	},

	computed: {
		isPopup() {
			const { value = {} } = this;

			if (!Object.keys(value).length) {
				return false;
			}

			const filterList = [0, 1];

			return filterList.includes(value?.codeUseStatus);
		},

		cycleLabel() {
			const { value = {} } = this;

			if (!Object.keys(value).length) {
				return;
			}

			return (cycleTypeList.find((item) => item.type === value.cycleType) || {})?.label || '';
		},

		downloadCount() {
			const { value = {} } = this;

			return value?.download || '';
		}
	},

	data() {
		return {
			bgImage
		};
	},

	methods: {
		maskClosePopup() {
			const { value } = this;
			this.$emit('clickCallBack', value);

			this.$emit('input', {});
		},

		openMaterial() {
			const { jumpRoute } = this;

			uni.switchTab({
				url: jumpRoute,
				success: (e) => {
					this.maskClosePopup();
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
@import '@/style/common.scss';

.layout {
	position: fixed;
	top: 0;
	left: 0;
	width: 100vw;
	height: 100vh;
	z-index: 999999;
	background: rgba(0, 0, 0, 0.6);

	&-main {
		position: absolute;
		top: 50%;
		left: 50%;
		height: auto;
		overflow: hidden;
		width: calc(100vw - 178rpx);

		border-radius: 24rpx;
		transform: translate(-50%, -50%);

		&-content {
			height: 798rpx;
			background-size: 100% 100%;
			background-repeat: no-repeat;

			padding: 524rpx 0 46rpx;

			&-hint {
				padding: 0 67rpx;

				color: #e1a57a;
				font-size: 32rpx;
				text-align: center;
				font-weight: 600;
				line-height: 48rpx;

				text {
					display: inline-block;

					width: 44rpx;
					height: 40rpx;
					line-height: 40rpx;
					background: #f5894e;
					border-radius: 14rpx;
					border: 2rpx solid #fff;

					color: #fff;
					margin: 0 4rpx;
				}
			}

			&-button {
				margin-top: 50rpx;
				padding: 0 40rpx;

				button {
					@include Db_commonButton(88rpx, 24rpx);

					background: #ff732d;

					&:active {
						background: rgba(255, 115, 45, 0.7);
					}
				}
			}
		}

		&-close {
			text-align: center;

			image {
				width: 48rpx;
				height: 48rpx;
				margin: 30rpx;
			}
		}
	}
}
</style>
